<template>
  <div class="table-wrapper">
    <div class="search-container">
      <div class="search-wrapper">
        <span class="label">楼宇：</span>
        <el-cascader size="mini" class="large-select" v-model="params.room_id" :options="buildingList" placeholder="请选择楼宇"
                     :props="{ checkStrictly: true, emitPath: false, multiple: false }"
                     collapse-tags
                     clearable>
        </el-cascader>
      </div>
      <div class="search-wrapper">
        <span class="label">时间类型：</span>
        <el-select size="mini" v-model="params.time_type" placeholder="请选择时间类型" @change="changeTimeType">
          <el-option v-for="item in timeTypeList" :key="item.text" :label="item.text" :value="item.value"></el-option>
        </el-select>
      </div>
      <div class="search-wrapper">
        <span class="label">开始时间：</span>
        <el-date-picker
          size="mini"
          v-model="params.start_time"
          :type="params.time_type===1?'date':(params.time_type===2?'month':'year')"
          :value-format="params.time_type===1?'yyyy-MM-dd':(params.time_type===2?'yyyy-MM':'yyyy')"
          placeholder="选择开始时间"
        >
        </el-date-picker>
      </div>
      <div class="search-wrapper">
        <span class="label">结束时间：</span>
        <el-date-picker
          size="mini"
          v-model="params.end_time"
          :type="params.time_type===1?'date':(params.time_type===2?'month':'year')"
          :value-format="params.time_type===1?'yyyy-MM-dd':(params.time_type===2?'yyyy-MM':'yyyy')"
          placeholder="选择结束时间"
        >
        </el-date-picker>
      </div>
    </div>
    <div class="search-container">
      <div class="search-wrapper">
        <span class="label">类型：</span>
        <el-select size="mini" v-model="params.type" placeholder="请选择类型">
          <el-option v-for="item in TypeList" :key="item.text" :label="item.text" :value="item.value"></el-option>
        </el-select>
      </div>
      <div class="search-wrapper">
        <span class="label">度数：</span>
        <el-input v-model="params.number" size="mini" class="common-input" placeholder="请输入度数"></el-input>
      </div>
      <div class="search-wrapper">
        <el-button @click="getRoomChartList" icon="el-icon-search" size="mini" >查询</el-button>
      </div>
    </div>
    <el-table
      :data="data"
      border
      height="calc(100% - 160px)"
      style="width: 100%">
      <el-table-column
        prop="room_name"
        label="楼宇">
      </el-table-column>
      <el-table-column
        prop="room_no"
        label="房间号">
      </el-table-column>
      <el-table-column
        prop="meter_no"
        label="电表编号">
      </el-table-column>
      <el-table-column
        width="200"
        prop="total_pp"
        label="总电量">
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :current-page="params.page"
      :page-size="15"
      :pager-count="5"
      @current-change="changePage"
      :total="params.total">
    </el-pagination>
  </div>
</template>

<script>
import portal from '@/api/portal'

export default {
  name: 'FjydList',
  data () {
    return {
      params: {
        page: 1,
        start_time: null,
        end_time: null,
        time_type: null,
        type: null,
        number: null,
        room_id: null
      },
      buildingList: [],
      timeTypeList: [
        {
          text: '天',
          value: 1
        },
        {
          text: '月',
          value: 2
        },
        {
          text: '年',
          value: 3
        }
      ],
      TypeList: [
        {
          text: '大于',
          value: 1
        },
        {
          text: '小于',
          value: 2
        }
      ],
      data: []
    }
  },
  created () {
    this.getRoomClass()
  },
  computed: {
  },
  mounted () {
  },
  methods: {
    // 房间用的查询列表
    async getRoomChartList () {
      if (!this.params.room_id) {
        this.$message.warning('请选择楼宇')
        return
      }
      if (!this.params.time_type) {
        this.$message.warning('请选择时间类型')
        return
      }
      if (!this.params.start_time) {
        this.$message.warning('请选择开始时间')
        return
      }
      if (!this.params.end_time) {
        this.$message.warning('请选择结束时间')
        return
      }
      if (!this.params.type) {
        this.$message.warning('请选择符号')
        return
      }
      if (!this.params.number) {
        this.$message.warning('请输入度数')
        return
      }
      const params = Object.assign({}, this.params)
      const result = await portal.getRoomChartList(params)
      if (result.data) {
        this.data = result.data
      }
    },
    // 获取楼宇
    async getRoomClass () {
      const result = await portal.getRoomClass()
      if (result.data) {
        const building = result.data
        building.forEach(e => {
          e.label = e.name
          e.value = e.id
          e.disabled = true
          e.children = e.child
          if (e.children) {
            e.children.forEach(el => {
              el.label = el.name
              el.value = el.id
              el.children = el.child
              delete el.children
              if (el.children) {
                el.children.forEach(ele => {
                  ele.label = el.name
                  ele.value = el.id
                  delete ele.children
                })
              }
            })
          }
        })
        this.buildingList = building
      }
    },
    changeTimeType () {
      this.params.start_time = null
      this.params.end_time = null
    },
    changePage (val) {
      this.params.page = val
      this.getRoomChartList()
    }
  }
}
</script>

<style scoped lang="scss">

</style>
